﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Deanery.Controller.Directories
{
    class ECTSOcinkaController
    {
        private readonly deaneryEntities _db = new deaneryEntities();

        /// <summary>
        /// This method transfers columns name in ukrainian for view table ECTS_Ocinka
        /// </summary>
        /// <returns>Array of string with columns names</returns>
        public Array ColumnName()
        {
            string[] columnName = { "ID", "Сума балів", "Оцінка ECTS", "Національна", "Вид контролю" };
            return columnName;
        }

        /// <summary>
        /// This method transfers columns data type in table ECTS_Ocinka
        /// </summary>
        /// <returns>Array of string with columns data type</returns>
        public Array ColumnValueType()
        {
            string[] columnDataType = { "guid", "int", "string", "string", "guid" };
            return columnDataType;
        }

        /// <summary>
        /// This method select all reccords from table ECTS_Ocinka
        /// PROBLEM: if will be many reccords in this table, method can slove the application
        /// SOLVE: stable quantities assync selection 
        /// </summary>
        /// <returns>Set of IQueryable</returns>
        public IQueryable GetAll()
        {
            var list = _db.ECTS_Ocinka.Join(_db.Type_Test, f => f.id_type_test, t => t.id_type_test,
                                            (f, t) =>
                                            new
                                                {
                                                    f.id_ECTS_ocinka,
                                                    f.int_sum_ocinka,
                                                    f.nv_ocinka_ECTS,
                                                    f.nv_nac_shkala,
                                                    t.nv_name_test
                                                });
            return list;
        }

        /// <summary>
        /// This method select all tests name from table Type_Test
        /// idea of this method: to return data from all conected tables with this one
        /// </summary>
        /// <returns>Array(every item is test name)</returns>
        public Array GetConectedData()
        {
            var list = from t in _db.Type_Test select t.nv_name_test;
            return list.ToArray();
        }

        /// <summary>
        /// This method performs INSERT one record to DB in table ECTS_Ocinka
        /// PROBLEM: method returns nothing
        /// </summary>
        /// <param name="reccord">ArrayList(one item - one cell value) with data to insert</param>
        public void Insert(ArrayList reccord)
        {
            var typeTestObj = new TypeTestController();
            var obj = new ECTS_Ocinka
            {
                id_ECTS_ocinka = GenerateGuid(),
                int_sum_ocinka = Convert.ToInt32(reccord[0]),
                nv_ocinka_ECTS = reccord[1].ToString(),
                nv_nac_shkala = reccord[2].ToString(),
                id_type_test = typeTestObj.FindGuidByName(reccord[3].ToString())

            };
            _db.ECTS_Ocinka.AddObject(obj);
            _db.SaveChanges();
        }

        /// <summary>
        /// This method performs UPDATE one record to DB in table ECTS_Ocinka
        /// PROBLEM: method returns nothing
        /// </summary>
        /// <param name="updateGuid">guid value to search reccord in table</param>
        /// <param name="reccord">ArrayList(one item - one cell value) with data to update</param>
        public void Update(Guid updateGuid, ArrayList reccord)
        {
            var typeTestObj = new TypeTestController();
            var obj = (_db.ECTS_Ocinka.Where(t => t.id_ECTS_ocinka == updateGuid)).Single();
            obj.int_sum_ocinka = Convert.ToInt32(reccord[0]);
            obj.nv_ocinka_ECTS = reccord[1].ToString();
            obj.nv_nac_shkala = reccord[2].ToString();
            obj.id_type_test =typeTestObj.FindGuidByName(reccord[3].ToString());
            _db.SaveChanges();
        }

        /// <summary>
        /// This method generates new guid
        /// </summary>
        /// <returns>New guid</returns>
        public Guid GenerateGuid()
        {
            var guid = Guid.NewGuid();
            return guid;
        }
    }
}
